package com.huaboot.generator.utils;

import com.huaboot.commont.exception.BusinessException;
import com.huaboot.commont.exception.ErrorCode;
import com.huaboot.generator.config.template.TemplateInfo;
import org.springframework.util.StreamUtils;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * description: 获取模板内容工具类
 * author: ff
 */
public  class TemplateInfoUtils {
    public static List<TemplateInfo> templateInfoList;

    private final static String TEMPLATE = "/template/hua-boot/";

    //初始化加载模板相关路径
    static {
        String[] templateNameArray = {
            "java/Controller.java.ftl",
            "java/Service.java.ftl",
            "java/ServiceImpl.java.ftl",
            "java/Query.java.ftl",
            "java/Entity.java.ftl",
            "java/VO.java.ftl",
            "java/Convert.java.ftl",
            "java/Mapper.java.ftl",
            "xml/Mapper.xml.ftl",
            "sql/menu.sql.ftl",
            "vue/api.ts.ftl",
            "vue/index.vue.ftl",
            "vue/add-or-update.vue.ftl"
        };
        String[] generatorPathArray  = {
            "${backendPath}/src/main/java/${packagePath}/${moduleName}/controller/${ClassName}Controller.java",
            "${backendPath}/src/main/java/${packagePath}/${moduleName}/service/${ClassName}Service.java",
            "${backendPath}/src/main/java/${packagePath}/${moduleName}/service/impl/${ClassName}ServiceImpl.java",
            "${backendPath}/src/main/java/${packagePath}/${moduleName}/query/${ClassName}Query.java",
            "${backendPath}/src/main/java/${packagePath}/${moduleName}/entity/${ClassName}Entity.java",
            "${backendPath}/src/main/java/${packagePath}/${moduleName}/vo/${ClassName}VO.java",
            "${backendPath}/src/main/java/${packagePath}/${moduleName}/convert/${ClassName}Convert.java",
            "${backendPath}/src/main/java/${packagePath}/${moduleName}/mapper/${ClassName}Mapper.java",
            "${backendPath}/src/main/resources/mapper/${moduleName}/${ClassName}Mapper.xml",
            "${backendPath}/menu/${tableName}_menu.sql",
            "${frontendPath}/src/api/${moduleName}/${functionName}.ts",
            "${frontendPath}/src/views/${moduleName}/${functionName}/index.vue",
            "${frontendPath}/src/views/${moduleName}/${functionName}/add-or-update.vue"
        };
        // 执行初始化操作，仅执行一次
        templateInfoList = new ArrayList<>();
        // 添加元素到缓存列表
        for (int i = 0; i < templateNameArray.length; i++) {
            TemplateInfo templateInfo = new TemplateInfo();
            templateInfo.setTemplateName(templateNameArray[i]);
            templateInfo.setGeneratorPath(generatorPathArray[i]);
            templateInfoList.add(templateInfo);
        }

    }


    public List<TemplateInfo> getGeneratorConfig(List<TemplateInfo> templateInfoList) {
        try {
            for (TemplateInfo templateInfo : templateInfoList) {
                // 模板文件
                InputStream isTemplate = this.getClass().getResourceAsStream(TEMPLATE + templateInfo.getTemplateName());
                if (isTemplate == null) {
                    throw new BusinessException(ErrorCode.GENERATOR_ERROR, "模板文件 " + templateInfo.getTemplateName() + " 不存在");
                }
                // 读取模板内容
                String templateContent = StreamUtils.copyToString(isTemplate, StandardCharsets.UTF_8);

                templateInfo.setTemplateContent(templateContent);
            }
        }catch (Exception e){
            throw new BusinessException(ErrorCode.GENERATOR_ERROR,"读取模板失败");
        }
        return templateInfoList;
    }


}
